package com.lirh.multiple.demo.controller;

import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@RestController
public class FileController {
    /**
     *处理文件上传
     */


    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file){
        System.out.println(file.getName());
        System.out.println(file.getOriginalFilename());
        return "ok";


    }
    @GetMapping("/down/{filename}")
    public void down(@PathVariable("filename") String filename, HttpServletResponse response) throws IOException {
        System.out.println(filename);

       response.setContentType("application/force-download");
       response.addHeader("Content-Disposition","attachment;fileName=" + filename);

        Path path = Paths.get(ResourceUtils.getURL("classpath:").getPath()  + filename);
        System.out.println("file:"+ResourceUtils.getURL("classpath:").getPath() + File.separator + "a.text");
        System.out.println(path.getFileName());
        Files.copy(path,response.getOutputStream());


    }

}
